package icu.bughub.app.app.ui.components

import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Divider
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.layoutId
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.constraintlayout.compose.ConstraintLayout
import androidx.constraintlayout.compose.ConstraintSet
import androidx.constraintlayout.compose.Dimension
import coil.compose.AsyncImage
import com.google.accompanist.placeholder.PlaceholderHighlight
import com.google.accompanist.placeholder.material.placeholder
import com.google.accompanist.placeholder.material.shimmer
import icu.bughub.app.app.model.entity.VideoEntity

@Composable
fun VideoItem(
    video: VideoEntity,
    modifier: Modifier = Modifier,
    videoLoadSuccess: Boolean = false
) {

    val constraintSet = ConstraintSet {
        val thumbnailUrl = createRefFor("thumbnailUrl")
        val title = createRefFor("title")
        val type = createRefFor("type")
        val duration = createRefFor("duration")
        val divider = createRefFor("divider")

        constrain(thumbnailUrl) {
            start.linkTo(parent.start)
            centerVerticallyTo(parent) //垂直居中

            width = Dimension.value(115.5.dp)
        }

        constrain(title) {
            start.linkTo(thumbnailUrl.end, margin = 8.dp)
            end.linkTo(parent.end)
            width = Dimension.fillToConstraints //宽度填充父容器
        }

        constrain(type) {
            start.linkTo(title.start)
            bottom.linkTo(divider.top)
            width = Dimension.fillToConstraints     //宽度填充父容器
        }

        constrain(duration) {
            start.linkTo(type.end, margin = 30.dp)
            //end.linkTo(parent.end)
            bottom.linkTo(divider.top)
        }

        constrain(divider) {
            // top.linkTo(parent.bottom)
            bottom.linkTo(thumbnailUrl.bottom, margin = (-8).dp)
        }
    }

    //约束布局

    ConstraintLayout(
        constraintSet = constraintSet,
        modifier = modifier
            .fillMaxWidth()
            .padding(horizontal = 8.dp, vertical = 4.dp)
        //.then(Modifier.padding(horizontal = 12.dp))
    ) {

        AsyncImage(
            model = video.thumbnailUrl,
            contentDescription = null,
            contentScale = androidx.compose.ui.layout.ContentScale.Crop,
            modifier = Modifier
                .layoutId("thumbnailUrl")
                .aspectRatio(16f / 9f) //设置比例为16:9
                .clip(RoundedCornerShape(4.dp))
                .placeholder(
                    visible = !videoLoadSuccess,
                    color = Color.LightGray.copy(alpha = 0.5f),
                    highlight = PlaceholderHighlight.shimmer()
                )
        )

        Text(
            text = video.title,
            fontSize = 16.sp,
            color = Color(0xFF666666),
            maxLines = 2,
            overflow = TextOverflow.Ellipsis,
            modifier = Modifier
                .layoutId("title")
                .placeholder(
                    visible = !videoLoadSuccess,
                    color = Color.LightGray.copy(alpha = 0.5f),
                    highlight = PlaceholderHighlight.shimmer()
                )
        )

        Text(
            text = video.type, fontSize = 10.sp,
            color = Color(0xFF999999),
            maxLines = 1,
            modifier = Modifier
                .layoutId("type")
                .placeholder(
                    visible = !videoLoadSuccess,
                    color = Color.LightGray.copy(alpha = 0.5f),
                    highlight = PlaceholderHighlight.shimmer()
                )
        )

        Text(
            text = "时长：${video.duration}", fontSize = 10.sp,
            color = Color(0xFF999999),
            maxLines = 1,
            modifier = Modifier
                .layoutId("duration")
                .placeholder(
                    visible = !videoLoadSuccess,
                    color = Color.LightGray.copy(alpha = 0.5f),
                    highlight = PlaceholderHighlight.shimmer()
                )
        )


    }
    Divider(
        color = Color(0x66E5E5E5),
        modifier = Modifier
            .layoutId("divider")
    )


}